package edu.fit5046.mymonashmate.utils;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class Utils {

	public Utils() {

	}

	@SuppressWarnings("null")
	static public Location getLocation(Context context) {
		Location netLocation = null;
		Location gpsLocation = null;
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
			netLocation = locationManager
					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
			gpsLocation = locationManager
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (netLocation == null) {
			if (gpsLocation == null) {
				return null;
			} else {
				return gpsLocation;
			}
		} else {
			if (gpsLocation == null) {
				return netLocation;
			} else {
				Location tmpLocation = null;
				tmpLocation
						.setLatitude((netLocation.getLatitude() + gpsLocation
								.getLatitude()) / 2.0);
				tmpLocation
						.setLongitude((netLocation.getLongitude() + gpsLocation
								.getLongitude()) / 2.0);
				return tmpLocation;
			}
		}
	}

}
